<?php
namespace app\controller\open;

use app\service\AppInfoTableService;
use app\service\DescribeInfoService;
use app\service\ModuleInfoService;
use app\service\TutorialVideoService;
use app\validate\TutorialVideoValidate;
use app\validate\VendorValidate;
use share\utils\Helper;

class TutorialVideoController extends BaseOpenController{

    protected $beforeActionList = [
    ];
    public function initValidate(){
        $this->validate = new TutorialVideoValidate();
    }

    public function all_action(){
        $input = $this->check();
        $lang = strtoupper(trim($input['lang']));
        $videoInfos = TutorialVideoService::getAllByFilter([]);
        $moduleIds = Helper::arraySmartUnique(array_column($videoInfos,'model_id'));
        $moduleInfos = ModuleInfoService::getAll($moduleIds);
        $describeInfos = DescribeInfoService::getAllByFilter([
            'describe_id' => ['in',array_merge(
                Helper::arraySmartUnique(array_column($videoInfos,'title_id')),
                Helper::arraySmartUnique(array_column($moduleInfos,'describe_id'))
            )],
            'language' => $lang == 'CN' ? 'zh-CN' : 'us-EN',
        ]);

        $res = [];

        foreach($videoInfos as $video){
            $moduleId = $video['model_id'];
            $module = Helper::filterOne($moduleInfos,['id' => $moduleId]);
            $moduleDescribe = Helper::filterOne($describeInfos,['describe_id' => $module['describe_id']]);
            $titleDescribe = Helper::filterOne($describeInfos,['describe_id' => $video['title_id']]);
            if(empty($moduleDescribe) || empty($titleDescribe)){
                continue;
            }
            $tmp = $video;
            $tmp['moduleName'] = $moduleDescribe['describe_name'];
            $tmp['title'] = $titleDescribe['describe_name'];
            $res[] = $tmp;
        }        
        return self::ok($res);
    }
}